package com.hmall.api.config;

import com.hmall.api.fallback.ItemFallbackFactory;
import com.hmall.common.utils.UserContext;
import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DefaultFeignConfig {
    @Bean
    public Logger.Level feignLogLevel(){
        return Logger.Level.FULL;
    }

    @Bean
    public RequestInterceptor requestInterceptor(){
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate requestTemplate) {
                // 获取当前用户信息并添加到请求头
                Long userId = UserContext.getUser();
                if (userId != null) {
                    requestTemplate.header("userInfo", userId.toString());
                }
            }
        };
    }

    @Bean
    public ItemFallbackFactory itemFallbackFactory(){
        return new ItemFallbackFactory();
    }
}